﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using DA;

namespace BL.Membership
{
    public class OrderDeskMembership
    {
        private const string SESSION_COOKIE_NAME = "_user_key";
        private const string SESSION_COOKIE_GUID_KEY_NAME = "userid";

        public enum LoginStatus
        {
            Success,
            UserIsNotExist,
            PasswordNotMatch,
            UserIsLocked,
            UserIsNotActive,
            LoginFailed
        }

        public static LoginResponse Login(string userName, string password)
        {
            User loggedUser = null;
            UsersRepository usersRepository = new UsersRepository();

            loggedUser = usersRepository.GetEntitySet().SingleOrDefault(x => x.UserName.ToLower() == userName.ToLower());

            if (loggedUser == null)
                return new LoginResponse(LoginStatus.UserIsNotExist, null);
            else if (isLocked(loggedUser))
                return new LoginResponse(LoginStatus.UserIsLocked, loggedUser);
            else if (!loggedUser.CheckPassword(password))
            {
                loggedUser.LoginAttempts += 1;

                return new LoginResponse(LoginStatus.PasswordNotMatch, null);
            }
            else if (loggedUser.CheckPassword(password))
            {
                loggedUser.LastLogin = DateTime.Now;
                loggedUser.LoginAttempts = 0;

                //save
                CreatePermanentCookie(loggedUser, SESSION_COOKIE_NAME, SESSION_COOKIE_GUID_KEY_NAME, DateTime.Now.AddMinutes(20));
                return new LoginResponse(LoginStatus.Success, loggedUser);
            }
            else
            {
                //save
                return new LoginResponse(LoginStatus.LoginFailed, loggedUser);
            }
        }

        private static bool isLocked(User loggedUser)
        {
            return loggedUser.IsLocked();
        }

        public static void LogOut()
        {
            RemovePermanentCookie(SESSION_COOKIE_NAME, SESSION_COOKIE_GUID_KEY_NAME);
        }

        private static void RemovePermanentCookie(string cookieName, string cookieUserIdKey)
        {
            HttpContext.Current.Session.Remove(cookieName);
        }

        private static void CreatePermanentCookie(User user, string cookieName, string cookieUserIdKey, DateTime? cookieExpirationTime = null)
        {
            RemovePermanentCookie(SESSION_COOKIE_NAME, SESSION_COOKIE_GUID_KEY_NAME);
            HttpContext.Current.Session.Add(cookieName, new UserData(user));
        }

        private UserData _userData = null;
        public UserData GetCurrentLoggedInUser()
        {
            if (_userData == null)
            {
                if (HttpContext.Current != null && HttpContext.Current.Session != null)
                    _userData = HttpContext.Current.Session[SESSION_COOKIE_NAME] as UserData;
            }

            return _userData;
        }

        public bool IsCurrentUserAllowed(params UserPermission[] perms)
        {
            var user = GetCurrentLoggedInUser();

            if (user == null)
                return false;

            return user.IsUserAllowed(perms);
        }
    }
}
